package com.xxxx.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxxx.server.mapper.AdminMapper;
import com.xxxx.server.mapper.TclassMapper;
import com.xxxx.server.pojo.*;
import com.xxxx.server.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2022-03-13
 */
@RestController

@RequestMapping("/col/per/")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @Autowired
    private IRoleService iRoleService;

    @Autowired
    private ICollegeService collegeService;

    @Autowired
    private TclassMapper tclassMapper;

    @Autowired
    private IAdminRoleService iAdminRoleService;

    @ApiOperation(value = "获取所有学院信息")
    @GetMapping("/getcollege")
    public List<College> getAllCollege(){
        return collegeService.list();
    }

    @ApiOperation(value = "获取所有班级信息(根据学院id)")
    @GetMapping("/getclassbycollege/")
    public List<Tclass> getAllTclassByCollege(int id){
        QueryWrapper queryWrapper  = new QueryWrapper<>();
        queryWrapper.eq("cono",id);
        return tclassMapper.selectList(queryWrapper);
    }


    //学校查所有，学院查学院，辅导员查班级
    @ApiOperation(value = "获取所有用户信息(分页)")
    @GetMapping("/allAdmin")
    public RespPageBean getAllAdmin(@RequestParam(defaultValue = "1") Integer currentPage ,
                                    @RequestParam(defaultValue = "10")Integer size,
                                    Admin admin){
        Admin adminNow = (Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        int roleid = adminNow.getRoles().get(0).getId();
        return adminService.getAdminByPage(currentPage,size,admin,roleid,adminNow);
    }

    @ApiOperation(value = "更新当前用户信息")
    @PutMapping("/updateAdmin")
    public RespBean updateAdminInfo(@RequestBody Admin admin, Authentication authentication){
        if(adminService.updateById(admin)){
            SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(admin,null,authentication.getAuthorities()));
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation(value = "更新用户密码")
    @PutMapping("/updatePwd")
    public RespBean updateAdminPwd(@RequestBody Map<String,Object> info){
        String oldPwd = (String) info.get("oldPass");
        String newPwd = (String) info.get("pass");
        Integer adminId = (Integer) info.get("adminId");

        return adminService.updateAdminPwd(oldPwd,newPwd,adminId );
    }

    @ApiOperation(value = "获取所有角色信息")
    @GetMapping("/getRoles")
    public List<Role> getAllRoles(){
        return iRoleService.list();
    }

    @ApiOperation(value = "添加用户信息")
    @PostMapping("/addUser")
    public RespBean addUserInfo(@RequestBody Admin admin){
        return adminService.addUser(admin);
    }

    @ApiOperation(value = "删除个人信息")
    @DeleteMapping("/get/{id}")
    public RespBean deleteTdaka(@PathVariable Integer id){
        if (adminService.removeById(id)){
            return RespBean.success("删除成功!");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "更新个人信息")
    @PutMapping("/post/update")
    public RespBean updateTdaka(@RequestBody Admin admin){
        return adminService.updateUser(admin);
    }

    @ApiOperation(value = "批量删除个人信息")
    @DeleteMapping("/get/delete")
    public RespBean deleteTbackByIds(Integer[] ids){
        if (adminService.removeByIds(Arrays.asList(ids))){
            return RespBean.success("删除成功!");
        }
        return RespBean.error("删除失败！");
    }
}
